﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Text;
using YiDingCanWp7.Models;
using Newtonsoft.Json;

namespace YiDingCanWp7
{
    public partial class AddComment : PhoneApplicationPage
    {
        public string shopid = string.Empty;
        public AddComment()
        {
            InitializeComponent();
        }

        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            #region 检验提交的数据是否正确

            if (string.IsNullOrEmpty(commenttextbox.Text))
            {
                MessageBox.Show("请填写评价内容！");
                return;
            }
            #endregion
            performanceProgressBar.IsIndeterminate = true;
            var webClient = new WebClient();
            webClient.Headers["Content-type"] = "application/json";
            webClient.Encoding = Encoding.UTF8;
            StringBuilder postData = new StringBuilder();
            postData.Append("ReviewContent="+commenttextbox.Text.Trim()+"&DeliverSpeed="+deliverStarMark.Marked
            + "&Taste=" + tasteStarMark.Marked + "&QoS=" + serviceStarMark.Marked + "&shopid=" + shopid.ToString());
            webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(webClient_UploadStringCompleted);
           // webClient.UploadStringAsync(new Uri(BaseConfig.BaseUrl + "/Shop/AddShopReview"), "POST", postData.ToString());
            ShopReview review = new ShopReview() { 
                ShopId=int.Parse(shopid),
                ReviewContent=commenttextbox.Text.Trim(),
                DeliverSpeed=deliverStarMark.Marked,
                Taste=tasteStarMark.Marked,
                QoS=serviceStarMark.Marked,
                Created=DateTime.Now,
                Updated=DateTime.Now,
                DeviceUniqueId=PhoneUtils.GetDeviceUniqueId()
            };
            var json = JsonConvert.SerializeObject(review);
            webClient.UploadStringAsync(new Uri(BaseConfig.BaseUrl + "/Shop/AddShopReview"), "POST",json);
        }
        void webClient_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
        {
            try
            {
                MessageDTO message = JsonConvert.DeserializeObject<MessageDTO>(e.Result);

                MessageBox.Show(message.message);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally {
                performanceProgressBar.IsIndeterminate = false;
            }
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            shopid = this.NavigationContext.QueryString["shopid"].ToString();
            base.OnNavigatedTo(e);
        }

        
    }
}